/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.PhimPOJO;
import com.mysql.jdbc.CallableStatement;
import com.mysql.jdbc.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author kimloan
 */
public class PhimDAO {
    public boolean themPhim(PhimPOJO s) {
        boolean kq = false;
        String sql = String.format("INSERT INTO phim(idTheLoaiPhim,TenPhim,TomTat,ThoiLuong,TinhTrangPhim)"
                + " VALUES(%d,'%s','%s',%d,%d)",
                s.getLoaiPhim(), s.getTenPhim(), s.getTomTat(), s.getThoiLuong(), s.getTrangThai());
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        int n = helper.executeUpdate(sql);
        if (n == 1) {
            kq = true;//Them phim thanh cong
        }

        helper.close();
        return kq;
    }

    /**
     * Phuong Thuc Xoa Phim
     * @return
     */
    public boolean xoaPhim(String maPhim) {

        boolean kq = false;
        try {
            String sql = "delete from phim where idPhim=" + maPhim ;
            // String sql = "DELETE FROM Phim WHERE maPhim ='"+  maPhim+"'";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            int n = helper.executeUpdate(sql);
            if (n >= 1) {
                kq = true;//xoa thanh cong
            }
            helper.close();
            return kq;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return kq;
        }
    }

    /**
     * Phuong Thuc Cap Nhat Phim
     */
    public boolean capNhatPhim(PhimPOJO s) {
        boolean kq = false;
        
        String sql = String.format(
                "UPDATE phim SET idTheLoaiPhim=%d, TenPhim ='%s',TomTat='%s',ThoiLuong=%d,TinhTrangPhim=%d"
                + " WHERE idPhim=%d ",
                s.getLoaiPhim(), s.getTenPhim(), s.getTomTat(),s.getThoiLuong(), s.getTrangThai(),s.getMaPhim());
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        int n = helper.executeUpdate(sql);
        if (n == 1) {
            kq = true;//Cap Nhat Phim thanh cong
        }
        helper.close();
        return kq;
    }

    /**
     * Phuong Thuc Xem Danh Phim
     * @return
     */
    public ArrayList<PhimPOJO> layDanhPhimCacMaPhim() {
        ArrayList<PhimPOJO> ds = new ArrayList<PhimPOJO>();
        try {
            String sql = "SELECT * FROM phim";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                PhimPOJO s = new PhimPOJO();
                s.setMaPhim(rs.getInt("idPhim"));
                s.setLoaiPhim(rs.getInt("idTheLoaiPhim"));
                s.setTenPhim(rs.getString("TenPhim"));
                s.setTomTat(rs.getString("TomTat"));
                s.setThoiLuong(rs.getInt("ThoiLuong"));
                s.setTrangThai(rs.getInt("TinhTrangPhim"));
                ds.add(s);
            }
            helper.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return ds;
    }

    /**
     * Phuong Thuc Lay Thong Tin Phim
     * @param maPhim
     * @return
     */
    public PhimPOJO layThongTinPhim(String maPhim) {
        PhimPOJO phim = null;
        String sql = String.format("SELECT * FROM phim WHERE idPhim=%d", maPhim);
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            if (rs.next()) {
                phim = new PhimPOJO();
                phim.setMaPhim(rs.getInt("idPhim"));
                phim.setLoaiPhim(rs.getInt("idTheLoaiPhim"));
                phim.setTenPhim(rs.getString("TenPhim"));
                phim.setTomTat(rs.getString("TomTat"));
                phim.setThoiLuong(rs.getInt("ThoiLuong"));
                phim.setTrangThai(rs.getInt("TinhTrangPhim"));
               
            }
            helper.close();
        } catch (SQLException ex) {
            
            System.out.println(ex.getMessage());
        }
        return phim;
    }
    //Ham Lay danh Phim theo the loai

    public ArrayList<PhimPOJO> layDSPhim_TheoTheLoai(int maTheLoai) {

        ArrayList<PhimPOJO> dsphim = new ArrayList<PhimPOJO>();
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            Connection cn = (Connection) helper.getConnection();
            CallableStatement statement = (CallableStatement) cn.prepareCall("{call sp_LayDSPhim_TheoTheLoai(?)}");
            statement.setInt(1, maTheLoai);

            statement.execute();
            ResultSet rs = statement.getResultSet();
            while (rs.next()) {
                PhimPOJO phim = new PhimPOJO();
                phim.setMaPhim(rs.getInt("idPhim"));
                phim.setLoaiPhim(rs.getInt("idTheLoaiPhim"));
                phim.setTenPhim(rs.getString("TenPhim"));
                phim.setTomTat(rs.getString("TomTat"));
                phim.setThoiLuong(rs.getInt("ThoiLuong"));
                phim.setTrangThai(rs.getInt("TinhTrangPhim"));
                dsphim.add(phim);

            }
            helper.close();
            return dsphim;
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
            return dsphim;
        }
    }
    
    public ArrayList<PhimPOJO> timKiemPhim_TheoTenPhim(String _tenPhim) {

        ArrayList<PhimPOJO> ds = new ArrayList<PhimPOJO>();
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            Connection cn = (Connection) helper.getConnection();
            CallableStatement statement = (CallableStatement) cn.prepareCall("{call sp_LayPhimTheoTenPhim(?)}");
            statement.setString(1, _tenPhim);
            statement.execute();
            ResultSet rs = statement.getResultSet();
            while (rs.next()) {
                PhimPOJO phim = new PhimPOJO();
                phim.setMaPhim(rs.getInt("idPhim"));
                phim.setLoaiPhim(rs.getInt("idTheLoaiPhim"));
                phim.setTenPhim(rs.getString("TenPhim"));
                phim.setTomTat(rs.getString("TomTat"));
                phim.setThoiLuong(rs.getInt("ThoiLuong"));
                phim.setTrangThai(rs.getInt("TinhTrangPhim"));
                ds.add(phim);
            }
            helper.close();
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
        return ds;
    }
}
